package vn.gq.tym.model.item;

import org.json.JSONException;
import org.json.JSONObject;

import android.os.Parcel;
import android.os.Parcelable;

public class ChapterItem implements Parcelable {
	public static final String ITEM_NAME = "name";
	public static final String ITEM_ID = "chapid";
	
	private String name = "";
	private String chapid = "";

	public ChapterItem() {
		
	}
	
	public ChapterItem(Parcel source) {
		setChapId(source.readString());
		setName(source.readString());
	}
	
	public ChapterItem(String jsonStr) {
		try {
			JSONObject input = new JSONObject(jsonStr);
			name = input.getString(ITEM_NAME);
			chapid = input.getString(ITEM_ID);
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}
	
	@Override
	public int describeContents() {
		return 0;
	}

	@Override
	public void writeToParcel(Parcel dest, int flags) {
		dest.writeString(chapid);
		dest.writeString(name);
	}
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getChapId() {
		return chapid;
	}

	public void setChapId(String chapid) {
		this.chapid = chapid;
	}

	public static final Parcelable.Creator<ChapterItem> CREATOR = new Creator<ChapterItem>() {
		
		@Override
		public ChapterItem[] newArray(int size) {
			return new ChapterItem[size];
		}
		
		@Override
		public ChapterItem createFromParcel(Parcel source) {
			return new ChapterItem(source);
		}
	}; 
	
	public JSONObject toJsonObject() {
		JSONObject out = new JSONObject();
		try {
			out.put(ITEM_ID, chapid);
			out.put(ITEM_NAME, name);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return out;
	}
}
